package com.b511.swpu.cementbackend.util;

import com.b511.swpu.cementbackend.dto.output.NavigationOutDto;
import com.b511.swpu.cementbackend.dto.output.NavigationOutDto2;
import com.b511.swpu.cementbackend.dto.output.TreeOutputDto;
import com.b511.swpu.cementbackend.entity.SYSM_Company;
import com.b511.swpu.cementbackend.entity.SYSM_DataDict;
import com.b511.swpu.cementbackend.entity.SYS_Navigation;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class AnotherTreeUtil {

    public List<TreeOutputDto> getDataDictTree(List<SYSM_DataDict> dataDictPojos) {
        Map<String, TreeOutputDto> map1 = new TreeMap<>();
        List<Integer> code = new ArrayList<>();
        for (SYSM_DataDict pojo : dataDictPojos) {
            TreeOutputDto treeDto = new TreeOutputDto();
            treeDto.setLabel(pojo.getNAME());
            treeDto.setCode(pojo.getDICTCODE());
            map1.put(treeDto.getCode(), treeDto);

            if (code.indexOf(pojo.getDICTCODE().length()) == -1)
                code.add(pojo.getDICTCODE().length());
        }
        return returnList(map1, code);
    }

    public List<TreeOutputDto> getCompanyStructureTree(List<SYSM_Company> dataDictPojos) {
        Map<String, TreeOutputDto> map1 = new TreeMap<>();
        List<Integer> code = new ArrayList<>();
        for (SYSM_Company pojo : dataDictPojos) {
            TreeOutputDto treeDto = new TreeOutputDto();
            treeDto.setLabel(pojo.getNAME());
            treeDto.setCode(pojo.getCOMPANYCODE());
            map1.put(treeDto.getCode(), treeDto);

            if (code.indexOf(pojo.getCOMPANYCODE().length()) == -1)
                code.add(pojo.getCOMPANYCODE().length());
        }
        return returnList(map1, code);
    }

    public List<NavigationOutDto> getNavigationTree(List<SYS_Navigation> dataDictPojos) {
        Map<String, NavigationOutDto> map1 = new TreeMap<>();
        List<Integer> code = new ArrayList<>();
        for (SYS_Navigation pojo : dataDictPojos) {
            NavigationOutDto treeDto = new NavigationOutDto();
            treeDto.setName(pojo.getNAME());
            treeDto.setIndex(pojo.getNO());
            treeDto.setIcon(pojo.getICON());
            map1.put(pojo.getCODE(), treeDto);

            if (code.indexOf(pojo.getCODE().length()) == -1)
                code.add(pojo.getCODE().length());
        }
        return returnNavigationTree(map1, code);
    }

    public List<NavigationOutDto2> getNavigationTree2(List<SYS_Navigation> dataDictPojos) {
        Map<String, NavigationOutDto2> map1 = new TreeMap<>();
        List<Integer> code = new ArrayList<>();
        for (SYS_Navigation pojo : dataDictPojos) {
            NavigationOutDto2 treeDto = new NavigationOutDto2();
            treeDto.setId(pojo.getID());
            treeDto.setCode(pojo.getCODE());
            treeDto.setName(pojo.getNAME());
            map1.put(pojo.getCODE(), treeDto);

            if (code.indexOf(pojo.getCODE().length()) == -1)
                code.add(pojo.getCODE().length());
        }
        return returnNavigationTree2(map1, code);
    }

    public List<TreeOutputDto> returnList(Map<String, TreeOutputDto> map, List<Integer> code) {
        List<TreeOutputDto> result = new ArrayList<>();
        Collections.sort(code);
        for (Map.Entry<String, TreeOutputDto> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.length() == code.get(0))
                result.add(entry.getValue());
            else {
                TreeOutputDto treeDto = map.get(key.substring(0, code.get(code.indexOf(key.length()) - 1)));
                if (null == treeDto)
                    continue;
                if (null == treeDto.getChildren()) {
                    List<TreeOutputDto> tmp = new ArrayList<>();
                    tmp.add(entry.getValue());
                    treeDto.setChildren(tmp);
                } else
                    treeDto.getChildren().add(entry.getValue());
            }
        }
        return result;
    }

    public List<NavigationOutDto> returnNavigationTree(Map<String, NavigationOutDto> map, List<Integer> code) {
        List<NavigationOutDto> result = new ArrayList<>();
        Collections.sort(code);
        for (Map.Entry<String, NavigationOutDto> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.length() == code.get(0))
                result.add(entry.getValue());
            else {
                NavigationOutDto treeDto = map.get(key.substring(0, code.get(code.indexOf(key.length()) - 1)));
                if (null == treeDto)
                    continue;
                if (null == treeDto.getChild()) {
                    List<NavigationOutDto> tmp = new ArrayList<>();
                    tmp.add(entry.getValue());
                    treeDto.setChild(tmp);
                } else
                    treeDto.getChild().add(entry.getValue());
            }
        }
        return result;
    }

    public List<NavigationOutDto2> returnNavigationTree2(Map<String, NavigationOutDto2> map, List<Integer> code) {
        List<NavigationOutDto2> result = new ArrayList<>();
        Collections.sort(code);
        for (Map.Entry<String, NavigationOutDto2> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.length() == code.get(0))
                result.add(entry.getValue());
            else {
                NavigationOutDto2 treeDto = map.get(key.substring(0, code.get(code.indexOf(key.length()) - 1)));
                if (null == treeDto)
                    continue;
                if (null == treeDto.getChild()) {
                    List<NavigationOutDto2> tmp = new ArrayList<>();
                    tmp.add(entry.getValue());
                    treeDto.setChild(tmp);
                } else
                    treeDto.getChild().add(entry.getValue());
            }
        }
        return result;
    }



}
